package codechicken.wirelessredstone.core;

import java.util.ArrayList;

import net.minecraft.src.*;

import org.lwjgl.opengl.GL11;

public class GuiRedstoneWireless extends GuiScreen implements IGuiIndirectButtons, IGuiRemoteUseable
{
	public GuiRedstoneWireless(InventoryPlayer inventoryplayer, ITileWireless tileentityredstonewireless)
	{
		super();
		itile = tileentityredstonewireless;
		etile = (TileEntity)tileentityredstonewireless;
		selectedfreq = itile.getFreq();
		inventory = inventoryplayer;
	}
	
	public GuiRedstoneWireless(InventoryPlayer inventoryplayer)
	{
		super();
		item = (ItemWirelessFreq)inventoryplayer.getCurrentItem().getItem();
		selectedfreq = item.getItemFreq(inventoryplayer.getCurrentItem());
		inventory = inventoryplayer;
	}

	@SuppressWarnings("unchecked")
	public void initGui()
	{
		controlList.clear();
		
		if(largeGui)
		{
			controlList.add(new GuiButtonShorter(0, width / 2 + 100, height / 2 - 101, 14, 15, "x"));
			controlList.add(new GuiButton(1, width / 2 + 17, height / 2 - 85, 20, 20, "+"));
			controlList.add(new GuiButton(2, width / 2 - 37, height / 2 - 85, 20, 20, "-"));
			controlList.add(new GuiButton(3, width / 2 + 40, height / 2 - 85, 20, 20, "+10"));
			controlList.add(new GuiButton(4, width / 2 - 60, height / 2 - 85, 20, 20, "-10"));
			controlList.add(new GuiButton(5, width / 2 + 62, height / 2 - 85, 26, 20, "+100"));
			controlList.add(new GuiButton(6, width / 2 - 89, height / 2 - 85, 26, 20, "-100"));
			controlList.add(new GuiButton(7, width / 2 + 56, height / 2 - 63, 32, 20, "+1000"));
			controlList.add(new GuiButton(8, width / 2 - 89, height / 2 - 63, 32, 20, "-1000"));
			
			controlList.add(new GuiButtonShorter(9, width / 2 + 63, height / 2 + 62, 50, 16, "Simple"));
			
			setfreqbutton = new GuiButton(10, width / 2 + 25, height / 2 - 60, 26, 20, "set");
			controlList.add(setfreqbutton);
			
			setnamebutton = new GuiButton(11, width / 2 + 53, height / 2 - 22, 50, 20, "Set Name");
			controlList.add(setnamebutton);
			remnamebutton = new GuiButton(12, width / 2 + 53, height / 2 - 22, 50, 20, "Remove");
			controlList.add(remnamebutton);
			
			GuiButton selectnamebutton = new GuiButton(13, 0, 0, 0, 0, "");
			controlList.add(selectnamebutton);
			
			setcolourbutton = new GuiButton(14, width / 2 + 53, height / 2 + 35, 61, 20, "Phase Shift");
			controlList.add(setcolourbutton);
			
			GuiButton prevslotbutton = new GuiButtonArrow(15, width / 2 + 64, height / 2 + 17, 12, 12, 3);
			GuiButton nextslotbutton = new GuiButtonArrow(16, width / 2 + 90, height / 2 + 17, 12, 12, 1);
			controlList.add(prevslotbutton);
			controlList.add(nextslotbutton);
			
			textboxfreq = new GuiBetterTextField(this, fontRenderer, width / 2 - 20, height / 2 - 59, 40, 18, "");
			textboxfreq.setMaxStringLength(4);
			textboxfreq.setReturnButton(setfreqbutton);
			textboxfreq.setAllowedCharacters("0123456789");
			
			textboxname = new GuiBetterTextField(this, fontRenderer, width / 2 - 100, height / 2 - 20, 150, 16, "Initial");
			textboxname.setMaxStringLength(20);
			textboxname.setReturnButton(setnamebutton);
			textboxname.isFocused = false;
			
			slotnames = new GuiNameSlot(width / 2 - 100, height / 2 - 1, fontRenderer, this);
			slotnames.registerButtons(null, null, selectnamebutton);
			
			dyeslot = new GuiInvItemSlot(width / 2 + 75, height / 2 + 15, inventory, RedstoneEther.getColourSetters(), 14, this);
			dyeslot.registerButtons(nextslotbutton, prevslotbutton, setcolourbutton);
			
			updateFreqSetButton();
			reloadNameText();
			updateNames();
		}
		else
		{
			controlList.add(new GuiButtonShorter(0, width / 2 + 69, height / 2 - 77, 14, 15, "x"));
			controlList.add(new GuiButton(1, width / 2 + 13, height / 2 - 50, 20, 20, "+"));
		 	controlList.add(new GuiButton(2, width / 2 - 33, height / 2 - 50, 20, 20, "-"));
		 	controlList.add(new GuiButton(3, width / 2 + 35, height / 2 - 50, 20, 20, "+10"));
		 	controlList.add(new GuiButton(4, width / 2 - 55, height / 2 - 50, 20, 20, "-10"));
		 	controlList.add(new GuiButton(5, width / 2 + 57, height / 2 - 50, 26, 20, "+100"));
		 	controlList.add(new GuiButton(6, width / 2 - 83, height / 2 - 50, 26, 20, "-100"));
			controlList.add(new GuiButton(7, width / 2 + 39, height / 2 - 28, 32, 20, "+1000"));
			controlList.add(new GuiButton(8, width / 2 - 70, height / 2 - 28, 32, 20, "-1000"));
			
			controlList.add(new GuiButtonShorter(9, width / 2 - 27, height / 2 - 15, 54, 16, "Advanced"));
		}
		super.initGui();
	}

	public void updateScreen()
	{
		if(itile != null && mc.theWorld.getBlockTileEntity(etile.xCoord, etile.yCoord, etile.zCoord) != etile)//tile changed
		{
			mc.currentScreen = null;
    		mc.setIngameFocus();
		}
		super.updateScreen();
		
		if(largeGui)
		{
			updateColourSetButton();
			updateFreqSetButton();
			updateNames();
			textboxfreq.updateCursorCounter();
			textboxname.updateCursorCounter();
		}
	}
	
	public void drawScreen(int i, int j, float f)
	{
		drawDefaultBackground();		
		drawContainerBackground();
		
		super.drawScreen(i, j, f);//buttons
		
		GL11.glDisable(2896 /* GL_LIGHTING */);
		GL11.glDisable(2929 /* GL_DEPTH_TEST */);

		if(largeGui)
		{
			slotnames.drawScreen(i, j, f);
		
			String name = getSetObjectName();
			fontRenderer.drawString(name, width / 2 - fontRenderer.getStringWidth(name) / 2, height / 2 - 100, 0x303030);
			
			fontRenderer.drawString("Value", width / 2 - 49, height / 2 - 54, 0x404040);
			fontRenderer.drawString("Frequency Name", width / 2 - 65, height / 2 - 31, 0x404040);
			fontRenderer.drawString("Spectrum", width / 2 + 60, height / 2 + 2, RedstoneEther.getFreqColour(selectedfreq));
			
			int actualfreq = getSetObjectFreq();
			String sactualfreq = Integer.toString(actualfreq);
			String sselectedfreq = Integer.toString(selectedfreq);
	
			if(CoreProxy.isClient() && !RedstoneEther.canBroadcastOnFrequency(mc.thePlayer, selectedfreq))//jammed freq render
			{
				fontRenderer.drawString(sselectedfreq, width / 2 - fontRenderer.getStringWidth(sselectedfreq) / 2, height / 2 - 82, 0xFF4040);
				fontRenderer.drawString(sactualfreq, width / 2 - fontRenderer.getStringWidth(sactualfreq) / 2, height / 2 - 72, 0x4040FF);
			}
			else
			{
				fontRenderer.drawString(sactualfreq, width / 2 - fontRenderer.getStringWidth(sactualfreq) / 2, height / 2 - 79, 0xFFFFFF);
			}
			
			textboxfreq.drawTextBox();
			textboxname.drawTextBox();
			
			dyeslot.drawSlot(mc, fontRenderer);
		}
		else
		{
			String name = getSetObjectName();
			fontRenderer.drawString(name, width / 2 - fontRenderer.getStringWidth(name) / 2, height / 2 - 78, 0x303030);
			fontRenderer.drawString("Frequency", width / 2 - fontRenderer.getStringWidth("Frequency") / 2,  height / 2 - 67, 0x404040);
			
			int actualfreq = getSetObjectFreq();
			String sactualfreq = Integer.toString(actualfreq);
			String sselectedfreq = Integer.toString(selectedfreq);
	
			if(CoreProxy.isClient() && !RedstoneEther.canBroadcastOnFrequency(mc.thePlayer, selectedfreq))//jammed freq render
			{
				fontRenderer.drawString(sselectedfreq, width / 2 - fontRenderer.getStringWidth(sselectedfreq) / 2, height / 2 - 47, 0xFF4040);
				fontRenderer.drawString(sactualfreq, width / 2 - fontRenderer.getStringWidth(sactualfreq) / 2, height / 2 - 37, 0x4040FF);
			}
			else
			{
				fontRenderer.drawString(sactualfreq, width / 2 - fontRenderer.getStringWidth(sactualfreq) / 2, height / 2 - 44, 0xFFFFFF);
			}
		}

		GL11.glEnable(2896 /* GL_LIGHTING */);
		GL11.glEnable(2929 /* GL_DEPTH_TEST */);
	}
	
	private void updateFreqSetButton()
	{
		String s = textboxfreq.getText();
		int i;
		try
		{
			i = Integer.parseInt(s);
		}
		catch(NumberFormatException nfe)
		{
			i = -1;
		}
		
		setfreqbutton.enabled = i >= 0 && i <= RedstoneEther.numfreqs;
	}
	
	private void updateColourSetButton()
	{
		int selected = dyeslot.getSelectedIndex();
		if(selected == -1)
		{
			setcolourbutton.enabled = false;
			return;
		}
		if(selected == RedstoneEther.numcolours)
		{
			selected = -1;
		}
		setcolourbutton.enabled = selectedfreq != 0 && selected != RedstoneEther.getFreqColourId(selectedfreq);
	}
	
	private boolean nameIsUsed(String name)
	{
		ArrayList<String> names = RedstoneEther.getAllNames();
		return names.contains(name);
	}
	
	private void updateNames()
	{
		String name = RedstoneEther.getFreqName(selectedfreq);
		String tname = textboxname.getText();
		boolean set = name == null || name.equals("") || !name.equals(tname);
		boolean canset = !tname.equals("") && !nameIsUsed(tname) && selectedfreq != 0;
		
		setnamebutton.enabled = set && canset;
		setnamebutton.drawButton = set;
		remnamebutton.enabled = !set;
		remnamebutton.drawButton = !set;
		
		if(set)
		{
			slotnames.updateNameList(mc.thePlayer, textboxname.getText());
		}
		else if(name.equals(tname))
		{
			slotnames.updateNameList(mc.thePlayer, "");//set name can pick from box
			slotnames.removeName(tname);
		}
		else
		{
			slotnames.clearNameList();
		}
	}
	
	private void reloadNameText()
	{
		String name = RedstoneEther.getFreqName(selectedfreq);
		if(name == null)
		{
			name = "";
		}
		textboxname.setText(name);
	}
	
	private void pressIncrementalButton(int id)
	{
		switch(id)
		{
			case 1:
				selectedfreq++;
			break;
			case 2:
				selectedfreq--;
			break;
			case 3:
				selectedfreq += 10;
			break;
			case 4:
				selectedfreq -= 10;
			break;
			case 5:
				selectedfreq += 100;
			break;
			case 6:
				selectedfreq -= 100;
			break;
			case 7:
				selectedfreq += 1000;
			break;
			case 8:
				selectedfreq -= 1000;
			break;
		}
		if(selectedfreq > RedstoneEther.numfreqs)
		{
			selectedfreq = 0;
		}
		if(selectedfreq < 0)
		{
			selectedfreq = RedstoneEther.numfreqs;
		}
		
		setNewFreq();
	}
	
	private void pressSetFreqButton()
	{
		selectedfreq = Integer.parseInt(textboxfreq.getText());
		
		setNewFreq();
	}
	
	private void pressSetNameButton()
	{
		if(CoreProxy.isClient())
		{
			if(RedstoneEther.canBroadcastOnFrequency(mc.thePlayer, selectedfreq))
			{
				RedstoneEther.setFreqName(selectedfreq, textboxname.getText());
				CorePacketHandler.sendSetFreqInfo(selectedfreq, textboxname.getText(), RedstoneEther.getFreqColourId(selectedfreq));
			}
		}
		else
		{
			RedstoneEther.setFreqName(selectedfreq, textboxname.getText());
		}
	}
	
	private void pressRemNameButton()
	{
		if(CoreProxy.isClient())
		{
			if(RedstoneEther.canBroadcastOnFrequency(mc.thePlayer, selectedfreq))
			{
				RedstoneEther.setFreqName(selectedfreq, "");
				CorePacketHandler.sendSetFreqInfo(selectedfreq, "", RedstoneEther.getFreqColourId(selectedfreq));
				textboxname.setText("");
			}
		}
		else
		{
			RedstoneEther.setFreqName(selectedfreq, "");
			textboxname.setText("");
		}
	}
	
	private void pressColourButton()
	{
		int colourid = dyeslot.getSelectedIndex();
		if(colourid == -1)
		{
			return;
		}
		if(colourid == RedstoneEther.numcolours)
		{
			colourid = -1;
		}
		if(colourid == RedstoneEther.getFreqColourId(selectedfreq))
		{
			return;
		}
		if(CoreProxy.isClient())
		{
			if(RedstoneEther.canBroadcastOnFrequency(mc.thePlayer, selectedfreq))
			{
				dyeslot.decrementCurrentStack();
				RedstoneEther.setFreqColour(selectedfreq, colourid);
				CorePacketHandler.sendSetFreqInfo(selectedfreq, RedstoneEther.getFreqName(selectedfreq), colourid);
			}
		}
		else
		{
			dyeslot.decrementCurrentStack();
			RedstoneEther.setFreqColour(selectedfreq, colourid);
		}
	}
	
	private void selectSlotName()
	{
		String slotname = slotnames.getSelectedName();
		if(slotname == "")
		{
			return;
		}
		
		int freq = RedstoneEther.getFreqByName(slotname);
		if(freq != -1)
		{
			selectedfreq = freq;
			setNewFreq();
		}
	}
	
	public void buttonPressed(GuiButton guibutton)
	{
		actionPerformed(guibutton);
	}
	
	protected void actionPerformed(GuiButton guibutton)
	{
		if(guibutton.id == 0)
		{
            mc.displayGuiScreen(null);
            mc.setIngameFocus();
		}
		else if(guibutton.id >= 1 && guibutton.id <= 8)
		{
			pressIncrementalButton(guibutton.id);
		}
		else if(guibutton.id == 9)
		{
			largeGui = !largeGui;
			SaveManager.getInstance(0).setProperty("AdvancedGui", largeGui);
			initGui();
		}
		else if(guibutton.id == 10)
		{
			pressSetFreqButton();
		}
		else if(guibutton.id == 11)
		{
			pressSetNameButton();
		}
		else if(guibutton.id == 12)
		{
			pressRemNameButton();
		}
		else if(guibutton.id == 13)
		{
			selectSlotName();
		}
		else if(guibutton.id == 14)
		{
			pressColourButton();
		}
		else
		{
			dyeslot.actionPerformed(guibutton);
			slotnames.actionPerformed(guibutton);
		}
	}
	
	protected void keyTyped(char c, int i)
	{
		super.keyTyped(c, i);
		if(largeGui)
		{
			textboxname.textboxKeyTyped(c, i);
			textboxfreq.textboxKeyTyped(c, i);
			slotnames.keyTyped(c, i);
			dyeslot.keyTyped(c, i);
		}
	}
	
	protected void mouseClicked(int i, int j, int k)
	{
		if(largeGui)
		{
			textboxfreq.mouseClicked(i, j, k);
			textboxname.mouseClicked(i, j, k);
			slotnames.mouseClicked(i, j, k);
			dyeslot.mouseClicked(i, j, k);
		}
		super.mouseClicked(i, j, k);
	}
	
	protected void mouseMovedOrUp(int i, int j, int k)
	{
		super.mouseMovedOrUp(i, j, k);
		if(largeGui)
		{
			slotnames.mouseMovedOrUp(i, j, k);
		}
	}
	
	public void selectNextField()
	{
		textboxfreq.setFocused(textboxname.isFocused);
		textboxname.setFocused(!textboxname.isFocused);
	}
	
	private void setNewFreq()
	{
		if(!CoreProxy.isClient() || RedstoneEther.canBroadcastOnFrequency(mc.thePlayer, selectedfreq))
		{
			if(itile == null)
			{
				setItemFreq(selectedfreq);
			}
			else
			{
				itile.setFreq(selectedfreq);
			}
		}
		
		if(largeGui)
		{
			reloadNameText();
		}
	}
	
	private String getSetObjectName()
	{
		return itile == null ? item.getGuiName() : itile.getGuiName();
	}
	
	private int getSetObjectFreq()
	{
		return itile == null ? item.getItemFreq(inventory.getCurrentItem()) : itile.getFreq();
	}

	private void drawContainerBackground()
	{
		if(largeGui)
		{
			int i = mc.renderEngine.getTexture("/codechicken/wirelessredstone/core/guiWirelessLarge.png");
			GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
			mc.renderEngine.bindTexture(i);
			int posx = width / 2 - 118;
			int posy = height / 2 - 106;
			drawTexturedModalRect(posx, posy, 0, 0, 237, 190);
		}
		else
		{
			int i = mc.renderEngine.getTexture("/codechicken/wirelessredstone/core/guiWirelessSmall.png");
			 	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
			 	mc.renderEngine.bindTexture(i);
			 	int posx = width / 2 - 88;
				int posy = height / 2 - 83;
				drawTexturedModalRect(posx, posy, 0, 0, 176, 166);
				
		}
	}

	private void setItemFreq(int freq)
	{
		item.setItemFreq(inventory.currentItem, inventory.getCurrentItem(), freq);
	}
	
	public boolean doesGuiPauseGame()
	{
		return false;
	}
	
	private GuiBetterTextField textboxname;
	private GuiBetterTextField textboxfreq;
	private GuiNameSlot slotnames;
	private GuiInvItemSlot dyeslot;
	
	private GuiButton setfreqbutton;
	private GuiButton setnamebutton;
	private GuiButton remnamebutton;
	private GuiButton setcolourbutton;

	private int selectedfreq;

	protected TileEntity etile;
	protected ITileWireless itile;
	protected ItemWirelessFreq item;
	protected InventoryPlayer inventory;
	
	public static boolean largeGui = SaveManager.getInstance(0).getProperty("AdvancedGui", false);
}
